<?php

/**
 * Description of TeamDao_Class
 *
 * @author aziz
 */
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Team_Class.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/portal/domain/Country_Class.php';
require_once 'GenericDAO_Class.php';
require_once 'CountryDAO_Class.php';

class TeamDAO extends GenericDAO {

    public $countryDAO;

    public function __construct() {
        $this->countryDAO = new CountryDAO();
    }

    public function prepareService($module, $id, $object) {
        $this->type = TEAM_SERVICE_MOD;
        $this->module = $module;
        $this->id = $id;
        $this->object = $object;
        $this->__setUp();
    }

    public function loadAll() {
        $this->prepareService(SERVICE_ALL, null, null);
        $takimlar = $this->getRequest();
        for ($i = 0; $i < count($takimlar); $i++) {
            $team = new Team();
            $team->setId($takimlar[$i]->id);
            $team->setName($takimlar[$i]->name);
            $team->setDescription($takimlar[$i]->description);
            $team->setFoundedDate($takimlar[$i]->foundedDate);
            $team->setCountry($this->countryDAO->loadById($takimlar[$i]->country->id));
            $team->setStatus($takimlar[$i]->status);
            $teams[] = $team;
        }
        return $teams;
    }

    public function loadById($id) {
        $this->prepareService(SERVICE_SINGLE, $id, null);
        $takim = $this->getRequest();
        $team = new Team();
        $team->setId($takim->id);
        $team->setName($takim->name);
        $team->setDescription($takim->description);
        $team->setFoundedDate($takim->foundedDate);
        $team->setCountry($this->countryDAO->loadById($takim->country->id));
        $team->setStatus($takim->status);
        $team->setShortName($takim->shortName);
        $team->setAbbreviation($takim->abbreviation);
        return $team;
    }

    public function delete($id) {
        $this->prepareService(SERVICE_DELETE, $id, null);
        $this->deleteRequest();
    }

    public function upsert($team) {
        $this->prepareService(SERVICE_SAVE, null, $team);
        $this->postRequest();
    }

}

?>
